package com.ay.weixin.fdc.front.project.event;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;

import org.lizar.controller.Event;
import org.lizar.controller.intercepter.Before;
import org.lizar.controller.loader.BasicLoader;
import org.lizar.controller.loader.EventLoader;
import org.lizar.ioc.Container;
import org.lizar.util.StringHelper;

import com.ay.weixin.config.ProjectConfig;
import com.ay.weixin.config.types.FDCProjectStatus;
import com.ay.weixin.fdc.admin.category.domain.CategoryLoader;
import com.ay.weixin.fdc.boss.project.db.IbatisProjectDao;
import com.ay.weixin.fdc.boss.project.db.IbatisProjectGrahDao;
import com.ay.weixin.fdc.boss.project.db.IbatisProjectGrahImgDao;
import com.ay.weixin.fdc.boss.project.domain.FDCProject;
import com.ay.weixin.fdc.boss.project.domain.FDCProjectGrah;
import com.ay.weixin.fdc.boss.project.domain.FDCProjectGrahImg;

public class ProjectEvent extends Event implements Before{
	private CategoryLoader cl;
	private IbatisProjectDao project_dao;
	private IbatisProjectGrahDao grah_dao;
	private IbatisProjectGrahImgDao img_dao;
	
	@Override
	public void init_property() throws Exception {
		project_dao=Container.get(IbatisProjectDao.class);
		cl=Container.get(CategoryLoader.class);
		grah_dao=Container.get(IbatisProjectGrahDao.class);
		img_dao=Container.get(IbatisProjectGrahImgDao.class);
		
	}

	@Override
	public String default_handling_path() {
		return "/fdc/project/*";
	}

	@Override
	public void handle(EventLoader el) throws ServletException, IOException {
		long wxproject_id=el.queryPathInt(2);
		System.out.println("project_id:"+wxproject_id);	
		
		if(StringHelper.equals(el.queryPath(3), "")){
			project_list(el,wxproject_id);
		}else if(StringHelper.equals(el.queryPath(3), "detail")){
			project_detail(el,wxproject_id);
		}else if(StringHelper.equals(el.queryPath(3), "detail_samples")){
			project_detail_samples(el,wxproject_id);
		}else if(StringHelper.equals(el.queryPath(3), "grah")){
			grah(el,wxproject_id);
		}
		
	}
	
	
	
	private void grah(EventLoader el,long wxproject_id) throws IOException, ServletException{
		long grah_id=el.getLong("grah_id",-1);
		if(grah_id==-1){
			project_detail(el,wxproject_id);
			return ;
		}
		FDCProjectGrah grah=null;
		FDCProject project=null;
		List<FDCProjectGrahImg> ilist=null;
		long id=el.getLong("id");
		
		try {
			grah=grah_dao.get(grah_id);
			project=project_dao.get(id);
			ilist=img_dao.getProjectGrahImgs(grah_id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",el.i18("fdc.project.db.failure"));
		}
		
		el.setAttr("grah", grah);
		el.setAttr("project", project);
		el.setAttr("ilist", ilist);
		el.template("/WEB-INF/template/app/"+wxproject_id+"/fdc/project/huxin.vm");
	}
	
	private void project_list(EventLoader el,long wxproject_id) throws IOException, ServletException{
		int category_id=el.getInt("category");
		List<FDCProject> list=null;
		if(cl.get(category_id)==null){
			try {
				list=project_dao.getAllByFront(wxproject_id,FDCProjectStatus.onsale.getCode());
			} catch (SQLException e) {
				e.printStackTrace();
				el.setAttr("msg",el.i18("fdc.project.db.failure"));
			}
		}else{
			try {
				list=project_dao.getByCategoryByFront(wxproject_id,category_id,FDCProjectStatus.onsale.getCode());
			} catch (SQLException e) {
				e.printStackTrace();
				el.setAttr("msg",el.i18("fdc.project.db.failure"));
			}
		}
		
		el.setAttr("project_list", list);
		el.template("/WEB-INF/template/app/"+wxproject_id+"/fdc/project/index.vm");
	}
	
	private void project_detail_samples(EventLoader el,long project_id) throws IOException, ServletException{
		long id=el.getLong("id",-1);
		if(id==-1){
			project_list(el,project_id);
			return ;
		}
		FDCProject p=null;
		List<FDCProjectGrah> glist=null;
		try {
			p=project_dao.get(id);
			glist=grah_dao.getProjectGrahs(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg", el.i18("fdc.project.load.failured"));
		}
		if(glist!=null){
			for(FDCProjectGrah pg:glist){
				try {
					pg.setImages(img_dao.getProjectGrahImgs(pg.getId()));
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
		el.setAttr("project", p);
		el.setAttr("glist", glist);
		el.template("/WEB-INF/template/app/"+project_id+"/fdc/project/detail_samples.vm");
	}

	
	private void project_detail(EventLoader el,long project_id) throws IOException, ServletException{
		long id=el.getLong("id",-1);
		if(id==-1){
			project_list(el,project_id);
			return ;
		}
		FDCProject p=null;
		List<FDCProjectGrah> glist=null;
		try {
			p=project_dao.get(id);
			glist=grah_dao.getProjectGrahs(id);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg", el.i18("fdc.project.load.failured"));
		}
		if(glist!=null){
			for(FDCProjectGrah pg:glist){
				if(StringHelper.equals(pg.getType(), "images")){
					try {
						pg.setImages(img_dao.getProjectGrahImgs(pg.getId()));
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
		
		el.setAttr("project", p);
		el.setAttr("glist", glist);
		el.template("/WEB-INF/template/app/"+project_id+"/fdc/project/detail.vm");
	}

	@Override
	public void before(BasicLoader bl) throws ServletException, IOException {
		bl.setAttr("list_page_title", ProjectConfig.getStr(bl.queryPathInt(2),"fdc_project_setting.list_page_title"));
	}

}
